//
//  ScanQRCodeViewController.m
//  oc_demo
//
//  Created by haigui on 2021/8/5.
//

#import "ScanQRCodeViewController.h"
#import "QRCodePreviewView.h"
#import "QRCodeManager.h"
#import <AVFoundation/AVFoundation.h>
#import "MMNetworkRequest.h"
@interface ScanQRCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic, strong) QRCodePreviewView *previewView;
@property (nonatomic, strong) QRCodeManager *codeManager;

@property (nonatomic,strong)AVCaptureSession  *session;
@property (nonatomic,strong)AVCaptureVideoPreviewLayer *previewLayer;
@end

@implementation ScanQRCodeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    [self startScanning];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _previewView = [[QRCodePreviewView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _previewView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_previewView];

    __weak typeof(self) weakSelf = self;
    _codeManager = [[QRCodeManager alloc] initWithPreviewView:_previewView completion:^{
        [weakSelf startScanning];
    }];
    
//    _session = [[AVCaptureSession alloc]init];
//
//        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//        NSError *error = nil;
//        AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:&error];
//        if (error) {
//            NSLog(@"初始化input失败,%@",error);
//            return;
//        }
//        if ([_session canAddInput:input]) {
//            [_session addInput:input];
//            NSLog(@"添加input成功");
//        }else{
//            NSLog(@"添加input失败");
//            return;
//        }
//        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
//        if ([_session canAddOutput:output]) {
//            [_session addOutput:output];
//            NSLog(@"添加output成功");
//        }else{
//            NSLog(@"添加output失败");
//            return;
//        }
//
////        [output setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,nil]];
//    [output setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeQRCode,nil]];
//
//
//        dispatch_queue_t dispatchQueue = dispatch_queue_create("myQueue", NULL);
//
//        [output setMetadataObjectsDelegate:self queue:dispatchQueue];
//
//
//        _previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:_session];
//        [_previewLayer setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBarHeight-20)];
//        [_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//        [self.view.layer insertSublayer:_previewLayer atIndex:0];
//        //10.设置扫描范围
//    //    output.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.6f, 0.6f);
//        //10.开始扫描
//        [_session startRunning];
//        NSLog(@"开始扫描");
}
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    NSLog(@"come:%@",metadataObjects);
}
#pragma mark - Private functions

- (void)startScanning {
    __weak typeof(self) weakSelf = self;
    [_codeManager startScanningWithCallback:^(NSString * _Nonnull code) {
        [self showWaitHUD];
        [[MMNetworkRequest sharedNetworkingManage] GET:@"http://dev.app.cps.com.cn/api/v1/qrcode/deqr/decode" parameters:@{@"route":code} success:^(id  _Nonnull responseObject) {
            [self hideWaitHUD];
            NSLog(@"%@",responseObject);
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"cpsapp://%@",responseObject]];
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                [[UIApplication sharedApplication] openURL:url];
            }
        } failure:^(NSError * _Nonnull error) {
            [self hideWaitHUD];
        }];
    } autoStop:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
